import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-11-30
 * Time: 9:30
 */
//函数式接口
@FunctionalInterface
interface Func {
    void test();
}

@FunctionalInterface
interface Func1 {
    void test(int a);
}

@FunctionalInterface
interface Func2 {
    void test(int a, int b);
}


@FunctionalInterface
interface Func3 {
    int test();
}

@FunctionalInterface
interface Func4 {
    int test(int a);
}

@FunctionalInterface
interface Func5 {
    int test(int a, int b);
}

public class Lambda {
    public static void main(String[] args) {
        System.out.println("=======================");
        int a = 10;
        //a = 999999;（错误）
        //匿名内部类
        Func func = new Func() {
            @Override
            public void test() {
                //获取到的a不能是修改后的a
                //a = 999999;（错误）
                System.out.println("is me!" + a);
            }
        };
        func.test();
        //lambda
        System.out.println("======无参数无返回值======");
        //同理，a不能是修改后的a
        Func func1 = () -> System.out.println("oh ~ is me!" + a);
        func1.test();
        System.out.println("=====一个参数无返回值=====");
        Func1 func2 = x -> System.out.println(x);
        func2.test(10);
        System.out.println("=====二个参数无返回值=====");
        Func2 func3 = (x, y) -> System.out.println(x + y);
        func3.test(10, 20);
        System.out.println("======无参数有返回值======");
        Func3 func4 = () -> 10;
        System.out.println(func4.test());
        System.out.println("======一参数有返回值======");
        Func4 func5 = x -> 2 * x;
        System.out.println(func5.test(10));
        System.out.println("======二参数有返回值======");
        Func5 func6 = (x, y) -> x + y;
        System.out.println(func6.test(10, 20));
        System.out.println("=======优先级队列========");
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        //使用lambda
        PriorityQueue<Integer> maxHeap2 = new PriorityQueue<>((o1, o2) -> {
            return o1.compareTo(o2);
        });
        PriorityQueue<Integer> maxHeap3 = new PriorityQueue<>((o1, o2) -> o1.compareTo(o2));
        System.out.println("=======forEach遍历======");
        ArrayList<String> list = new ArrayList<>();
        list.add("b");
        list.add("a");
        list.add("c");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s + " ");
            }
        });
        System.out.println();
        //lambda
        list.forEach(s-> System.out.print(s + " "));
        System.out.println();

        Map<String, Integer> map = new HashMap<>();
        map.put("abc", 1);
        map.put("okf", 5);
        map.put("vbn", 9);
        map.put("ijn", 2);
        map.put("uhg", 6);
        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println(s + ": " + integer);
            }
        });
        System.out.println();
        System.out.println();
        //lambda
        map.forEach((s, x)-> System.out.println(s + ": " + x));
        System.out.println("=======sort比较=========");
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //lambda
        list.sort((o1, o2) -> o1.compareTo(o2));
        list.forEach(s -> System.out.print(s + " "));
        System.out.println();
        list.sort((o1, o2) -> o2.compareTo(o1));
        list.forEach(s -> System.out.print(s + " "));
    }
}
